package com.ss.android.common.load;

import android.os.Message;

import java.lang.ref.WeakReference;

public final class AsyncLoader<K, T, E, V, R> extends AbsAsyncLoader<K, T, E, V, R, V, Node<K, T, E, V, R>> {
    public interface LoaderProxy<K, T, E, V, R> {
        R doInBackground(K key, T param, E extra);

        void onLoaded(K key, T param, E extra, R result);
    }

    private final WeakReference<LoaderProxy<K, T, E, V, R>> mProxyRef;

    public AsyncLoader(int capacity, int maxWorker, LoaderProxy loaderProxy, boolean useMainLooper) {
        super((loaderProxy.getClass().getName() != null) ? capacity : 1, maxWorker, useMainLooper);
        this.mProxyRef = new WeakReference(loaderProxy);
    }

    public AsyncLoader(LoaderProxy loaderProxy) {
        this(20, 3, loaderProxy);
    }

    public AsyncLoader(int capacity, int maxWorker, LoaderProxy loaderProxy) {
        this(capacity, maxWorker, loaderProxy, false);
    }

    protected final Node<K, T, E, V, R> makeNode() {
        return new Node<>();
    }

    protected final R doInBackground(K key, T param, E extra) {
        LoaderProxy<K, T, E, V, R> loaderProxy = this.mProxyRef.get();
        return loaderProxy != null ? loaderProxy.doInBackground(key, param, extra) : null;
    }

    protected final void onLoaded(K key, T param, E extra, V obj, R result) {
        LoaderProxy<K, T, E, V, R> loaderProxy = this.mProxyRef.get();
        if (loaderProxy != null) {
            loaderProxy.onLoaded(key, param, extra, result);
        }
    }

    public final void clearQueue() {
        super.clearQueue();
    }

    public final void stop() {
        super.stop();
        this.mProxyRef.clear();
    }

    public final void pause() {
        super.pause();
    }

    public final void resume() {
        super.resume();
    }

    public final void handleMsg(Message message) {
        super.handleMsg(message);
    }
}

